package com.ruoyi.web.controller.commerce;

import java.util.Collection;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.commerce.domain.vo.CommerceProductsVo;
import com.ruoyi.commerce.domain.bo.CommerceProductsBo;
import com.ruoyi.commerce.service.ICommerceProductsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品
 *
 * @author ruoyi
 * @date 2023-12-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/commerce/products")
public class CommerceProductsController extends BaseController {

    private final ICommerceProductsService iCommerceProductsService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("commerce:products:list")
    @GetMapping("/list")
    public TableDataInfo<CommerceProductsVo> list(CommerceProductsBo bo, PageQuery pageQuery) {
        return iCommerceProductsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出全部商品列表
     */
    @SaCheckPermission("commerce:products:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(CommerceProductsBo bo, HttpServletResponse response) {
        List<CommerceProductsVo> list = iCommerceProductsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", CommerceProductsVo.class, response);
    }

    /**
     * 导出选择商品列表
     */
    @SaCheckPermission("commerce:products:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{productIds}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> productIds, HttpServletResponse response) {
        List<CommerceProductsVo> list = iCommerceProductsService.queryByIds(productIds);
        ExcelUtil.exportExcel(list, "商品", CommerceProductsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param productId 主键
     */
    @SaCheckPermission("commerce:products:query")
    @GetMapping("/{productId}")
    public R<CommerceProductsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long productId) {
        return R.ok(iCommerceProductsService.queryById(productId));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("commerce:products:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommerceProductsBo bo) {
        return toAjax(iCommerceProductsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("commerce:products:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommerceProductsBo bo) {
        return toAjax(iCommerceProductsService.updateByBo(bo));
    }

    /**
     * 修改状态
     */
    @SaCheckPermission("commerce:products:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@Validated(EditGroup.class) @RequestBody CommerceProductsBo bo) {
        return toAjax(iCommerceProductsService.changeStatus(bo));
    }

    /**
     * 删除商品
     *
     * @param productIds 主键串
     */
    @SaCheckPermission("commerce:products:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] productIds) {
        return toAjax(iCommerceProductsService.deleteWithValidByIds(Arrays.asList(productIds), true));
    }
}
